#include <unistd.h>

#include<iostream>
#include<memory>
#include<vector>
#include<array>

#include<sys/wait.h>
using namespace std;
static void print_status(pid_t pid, int status)
{
    if (pid == 0) {
        cout << "[child return]" << endl;
    } else if (pid == -1) {
            cout << "errno = ";
            switch (errno) {
                case ECHILD: {
                    cout << "ECHILD" << endl;
                    break;
                case EINTR:
                    cout << "EINTR" << endl;
                    break;
                }
            }
    } else {
        cout << "[parent return] " << pid << endl;
    }
}
// 程序退出后执行 echo $? 显示的是父进程的退出码
int main(int argc, char *argv[])
{
    pid_t pid1;
    pid_t pid2;
    int status = 0;
    pid1 = fork();
    if (pid1 == 0) { // child
        cout << "[child] run" << endl;
        print_status(pid1, status);
        sleep(3); // 让父进程先退出
        exit(11);
    } else {
        cout << "[parent] run" << endl;
        // pid1 = waitpid(pid1, &status, 0);
        // print_status(pid1, status);
        // if (pid1 == -1) {
        //     exit(11);
        // }
        cout << "[parent] return" << endl;
        exit(10);
    }
}
